<template>
  <div id="bg">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限配置</el-breadcrumb-item>
    </el-breadcrumb>
    <div id="tab">
      <!-- 搜索栏 -->
      <div>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="角色名称">
            <el-input v-model="formInline.user" placeholder="请输入角色名称"></el-input>
          </el-form-item>
          <el-form-item label="资源权限">
            <el-select v-model="formInline.permission" placeholder="请选择">
              <el-option label="个人中心" value="个人中心"></el-option>
              <el-option label="信息中心" value="信息中心"></el-option>
              <el-option label="考勤管理" value="考勤管理"></el-option>
              <el-option label="会议管理" value="会议管理"></el-option>
              <el-option label="绩效管理" value="绩效管理"></el-option>
              <el-option label="员工管理" value="员工管理"></el-option>
              <el-option label="通讯录" value="通讯录"></el-option>
              <el-option label="系统管理" value="系统管理"></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="info" @click="onReset">重置</el-button>
        </el-form>
      </div>
      <el-divider></el-divider>
      <!-- 新增按钮 -->
      <!-- <el-button type="primary" @click="addVisible=true">新增</el-button> -->
      <!-- 表格数据 -->
      <div id="table">
        <el-table :data="tablesData" stripe style="width: 100%">
          <el-table-column prop="r_id" label="权限编号" width="180">
          </el-table-column>
          <el-table-column prop="r_name" label="角色名称" width="180">
          </el-table-column>
          <el-table-column prop="r_permission" label="权限资源" width="400">
          </el-table-column>
          <el-table-column prop="r_operator" label="操作人">
          </el-table-column>
          <el-table-column prop="r_config" label="配置时间">
          </el-table-column>
          <el-table-column prop="operate" label="操作">
            <template slot-scope="scope">
              <!-- <el-button type="primary" @click="addClick(scope.row)" size="small">新增</el-button> -->
              <el-button type="warning" @click="handleClick(scope.row)" size="small">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div id="pages">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[5, 10, 15, 20]" :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
    <!-- 编辑弹框 -->
    <el-dialog title="权限配置编辑" :visible.sync="editVisible" width="30%" :before-close="handleClose">
      <el-form :model="formsDialog">
        <el-form-item label="权限编号" :label-width="formLabelWidth">
          <el-input v-model="formsDialog.nums" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="formsDialog.names" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="权限资源" :label-width="formLabelWidth">
          <el-input v-model="formsDialog.permissions" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="操作人" :label-width="formLabelWidth" disabled>
          <el-input v-model="formsDialog.operators" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="配置时间" :label-width="formLabelWidth">
          <el-date-picker type="date" placeholder="" value-format="yyyy-MM-dd" format="yyyy-MM-dd" v-model="formsDialog.dates" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOk">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除 -->
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      addVisible: false,
      editVisible: false,
      formInline: {
        user: '',
        permission: ''
      },
      tableData: [],
      formsDialog: {
        nums: '',
        names: '',
        permissions: '',
        operators: '',
        dates: ''
      },
      page: 1,
      pageSize: 5,
      total: 0,
      formLabelWidth: '120px'
    }
  },
  methods: {
    onSearch() {
      this.page = 1;
      this.$axios({
        url: '/system/systempermission/get',
        params: {
          name: this.formInline.user,
          permission: this.formInline.permission,
        },
      }).then((res) => {
        // console.log(res);
        this.tableData = res.data.data;
        this.total = res.data.total;
      })
    },
    onReset() {
      this.page = 1;
      this.getData();
      this.formInline.user = '';
      this.formInline.permission = '';
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.onSearch();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    handleClick(item) {
      this.editVisible = true;
      console.log(item);
      this.formsDialog = {
        nums: item.r_id,
        names: item.r_name,
        permissions: item.r_permission,
        operators: item.r_operator,
        dates: item.r_config,
      }
    },
    editOk() {
      this.editVisible = false;
      // console.log(this.formsDialog.permissions);
      this.$axios({
        url: '/system/systempermission/edit',
        method: 'post',
        data: {
          r_id: this.formsDialog.nums,
          r_name: this.formsDialog.names,
          permission: this.formsDialog.permissions,
          r_operator: this.formsDialog.operators,
          config: this.formsDialog.dates,
        }
      }).then((res) => {
        this.getData();
      })
    },
    getData() {
      this.$axios({
        url: '/system/systempermission/get',
        params: {
          name: '',
          permission: '',
        },
      }).then((res) => {
        this.tableData = res.data.data
        this.total = res.data.total;
      }).catch(error => {
        console.log(error);
      })
    }
  },
  created: function () {
    this.getData();
  },
  computed: {
    tablesData() {
      return this.tableData.slice((this.page - 1) * this.pageSize, this.page * this.pageSize)
    }
  }
}
</script>
<style scoped>
.el-row span {
  cursor: pointer;
}
.el-main,
#bg {
  height: 100%;
}
.el-breadcrumb {
  margin: 20px;
}
#closeBtn {
  text-align: right;
}
#bg {
  background-color: rgb(242, 242, 242);
}
#tab {
  padding: 30px;
  background-color: #fff;
  margin: 0 20px;
}
.el-breadcrumb {
  margin-top: 0;
  padding-top: 20px;
}
#pages {
  margin-top: 20px;
  text-align: right;
}
.el-select {
  width: 100%;
}
.el-form-item {
  padding-right: 40px;
}
</style>
